﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

namespace Interfaces.Programa
{
    public partial class BuscarPrograma : System.Web.UI.Page
    {
        
        Collection<Model.Programa> list;
        Collection<Model.Programa> cerrados;
        int gestion { get { return Convert.ToInt32(ViewState["gestion"]); } set { ViewState["gestion"] = value; } }
        char tipo{ get { return Convert.ToChar(ViewState["tipo"]); } set { ViewState["tipo"] = value; } }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["BuscarMaster"] != null)
            {
                string res = (string)Session["BuscarMaster"];
                this.MasterPageFile = res;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {              
                int index = Convert.ToInt32(Request.QueryString["Index"]);
                if (index==1)
                    MultiView1.ActiveViewIndex = 1;
                if(index==0)
                    MultiView1.ActiveViewIndex = 0;
                if (index == 2)
                    MultiView1.ActiveViewIndex = 2;
                if (index == 3)
                    MultiView1.ActiveViewIndex = 3;
                if (!IsPostBack)
                {
                    if (Request.QueryString["Gestion"] != null && Request.QueryString["Tipo"] != null)
                    {
                        gestion = Convert.ToInt32(Request.QueryString["Gestion"]);
                        tipo = Convert.ToChar(Request.QueryString["Tipo"]);
                    }
                    list = new Collection<Model.Programa>();
                    
                    long id = DataBase.Miembro2.BDMiembro.DevolverMiembroPorUsuario(User.Identity.Name).CI;
                    long idPresi = DataBase.Miembro2.BDMiembro.DevolverMiembroPorCargo("Presidenta").CI;
                    list = Processes.Programa.GestorPrograma.MostrarTodos(id,idPresi);
                    cerrados = Processes.Programa.GestorPrograma.MostrarTodosCerrados(id, idPresi);
                    DataList1.DataSource = list;
                    DataList1.DataBind();
                    DataList2.DataSource = list;
                    DataList2.DataBind();
                    DataList3.DataSource = list;
                    DataList3.DataBind();
                    DataList4.DataSource = cerrados;
                    DataList4.DataBind();
                }
            }
            catch (Exception ex)
            {
                errorlbl.Text = ex.Message;
           }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                long id = DataBase.Miembro2.BDMiembro.DevolverMiembroPorUsuario(User.Identity.Name).CI;
                long idPresi = DataBase.Miembro2.BDMiembro.DevolverMiembroPorCargo("Presidenta").CI;
                list = Processes.Programa.GestorPrograma.Buscar(TextBox1.Text, DropDownList1.SelectedValue, CheckBox1.Checked,id,idPresi);
                foreach (var item in list)
                {
                    if (item.CIDirectora == id || item.CreadoPor==id || id==idPresi)
                        item.Visible = true;
                    else
                        item.Visible = false;
                }
                {
                    DataList1.DataSource = list;
                    DataList1.DataBind();
                }
            }
            catch (Exception ex)
            {
                errorlbl.Text = ex.Message;
            }
        }
        protected void CancelClick(object sender, EventArgs e)
        {
            errorlbl.Text = "esta por ahi";

        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            Model.Programa prog = new Model.Programa();
            prog=DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
            Session["ver"] = prog;
            Response.Redirect("VerPrograma.aspx");
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            Model.Programa prog = new Model.Programa();
            prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
            Session["programa"] = prog;
            if(prog.Estado)
            
            Response.Redirect("ModificarPrograma.aspx");
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            Model.Programa prog = new Model.Programa();
            prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
            
            short ver;
            try
            {
                ver = Convert.ToInt16(Request.QueryString["Verificar"]);

                if (ver == 0)
                {
                    Session["programatarea"] = prog;
                    Response.Redirect("/Tareas/CrearTarea.aspx");

                }
                else if (ver == 1)
                {
                    Session["programatarea"] = prog;
                    Response.Redirect("/Tareas/BuscarTarea.aspx");
                }
                
                else if (ver == 3)
                {
                    Session["programatarea"] = prog;
                    Response.Redirect("/Tareas/ModificarTarea.aspx?Relacionado=" + Request.QueryString["Relacionado"] + "&ID=" + Request.QueryString["ID"]);
                }
            }
            catch (FormatException)
            {
                //enviar a una vista de error
            }
            
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                long id = DataBase.Miembro2.BDMiembro.DevolverMiembroPorUsuario(User.Identity.Name).CI;
                long idPresi = DataBase.Miembro2.BDMiembro.DevolverMiembroPorCargo("Presidenta").CI;
                list = Processes.Programa.GestorPrograma.Buscar(TextBox2.Text, DropDownList2.SelectedValue, CheckBox2.Checked,id,idPresi);
                {
                    DataList2.DataSource = list;
                    DataList2.DataBind();
                }
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }

        protected void CrearBtn_Click(object sender, EventArgs e)
        {
            LinkButton b= sender as LinkButton;
            Model.Programa prog = new Model.Programa();
            prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
            Session["evaPrograma"] = prog;
            Response.Redirect("/Evaluacion/CrearEvaluacion.aspx");
        }

        protected void ModificarBtn_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            Model.Programa prog = new Model.Programa();
            prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
            Session["evaProgramaMod"] = prog;
            Response.Redirect("/Evaluacion/ModificarEvaluacion.aspx");
        }

        protected void VerEvaluacionBtn_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            Model.Programa prog = new Model.Programa();
            prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
            Session["verEvaluacion"] = prog;
            Response.Redirect("/Evaluacion/VerEvaluacion.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                long id = DataBase.Miembro2.BDMiembro.DevolverMiembroPorUsuario(User.Identity.Name).CI;
                long idPresi = DataBase.Miembro2.BDMiembro.DevolverMiembroPorCargo("Presidenta").CI;
                list = Processes.Programa.GestorPrograma.Buscar(TextBox3.Text, DropDownList3.SelectedValue, CheckBox3.Checked,id,idPresi);
                {
                    DataList3.DataSource = list;
                    DataList3.DataBind();
                }
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            LinkButton cerrar = (LinkButton)e.Item.FindControl("LinkButton2");
            LinkButton modificar = (LinkButton)e.Item.FindControl("LinkButton3");
            Model.Programa prog = new Model.Programa();
            prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(cerrar.CommandArgument));
            if (prog.Estado)
            {
                prog = DataBase.Programa.BDPrograma.CerrarPrograma(prog);
                cerrar.Visible = false;
                modificar.Visible = false;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                long id = DataBase.Miembro2.BDMiembro.DevolverMiembroPorUsuario(User.Identity.Name).CI;
                long idPresi = DataBase.Miembro2.BDMiembro.DevolverMiembroPorCargo("Presidenta").CI;
                list = Processes.Programa.GestorPrograma.BuscarCerrados(TextBox4.Text, DropDownList4.SelectedValue, CheckBox4.Checked, id, idPresi);
                {
                    DataList4.DataSource = list;
                    DataList4.DataBind();
                }
            }
            catch (Exception ex)
            {
                Label2.Text = ex.Message;
            }
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton b = sender as LinkButton;
                Model.Programa prog = new Model.Programa();
                prog = DataBase.Programa.BDPrograma.ProgramasPorCodigo(Convert.ToInt64(b.CommandArgument));
                Session["ProgramaPresupuesto"] = prog;
                Response.Redirect("/Presupuesto/FormularioPresupuesto.aspx?Gestion=" + gestion + "&Tipo=" + tipo);
            }
            catch (Exception)
            { 
            }
        }
    }
}
